//
//  ConsultFamilyViewController.m
//  安好
//
//  Created by lianchuang on 14/11/27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ConsultFamilyViewController.h"
#import "DepartmentTableViewCell.h"
#import "AddFamilyViewController.h"
@interface ConsultFamilyViewController ()

@end

@implementation ConsultFamilyViewController
{
    NSInteger sel_count;
    NSDictionary *dicData;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self getMyFamily];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self hiddenActionView];
    self.famBlock(dicData);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.navigationItem.title=@"指定家人";
    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.hidden=YES;
    self.tableview.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableview.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
    UserModel * userModel = [[UserManager shareInstance]getUserInfo];
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];

    NSString * name= [userDefaults valueForKey:[NSString stringWithFormat:@"%@realname",userModel.uid]];
    NSString *myuid=userModel.uid;
    NSString *gender=userModel.gender;
    NSString *age=userModel.age;

    NSDictionary *dic=@{
        @"fid": @"0",//(家人id)
        @"gender": gender,//(性别 0:保密 1:男 2:女)
        @"type": @"0",//(关系id)
        @"realname":name,//(家人姓名)
        @"age": age,//(年龄 -1:未填写)
        @"relation": @"我",//(关系)
        @"mobile":[userDefaults valueForKey:[NSString stringWithFormat:@"%@mobile",myuid]]
        };
    dicData=[[NSDictionary alloc]init];
    dicData=dic;
    self.dataArray=[[NSMutableArray alloc] initWithObjects:dic,nil];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
/**
 *  返回
 */
- (void)handleLeftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  传输块
 *
 *  @param block 
 */
- (void)returnFamData:(FamilyBlock)block
{
    self.famBlock=block;
}

/**
 *  请求数据
 */
- (void)getMyFamily
{
    [self showActionView];
    NSString * getURL = [NSString stringWithFormat:@"%@question/myfamily/?platform=%@&version=%@",kBaseURL,kPlatform,kVersion];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:getURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hiddenActionView];
        self.tableview.hidden=NO;
        MyNSLog("%@", responseObject);
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
        if ([status isEqualToString:@"1"])
        {
            self.dataArray=[[NSMutableArray alloc]initWithArray:[responseObject valueForKey:@"data"]];
            UserModel * userModel = [[UserManager shareInstance]getUserInfo];
            NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
            NSString * name= [userdefault valueForKey:[NSString stringWithFormat:@"%@realname",userModel.uid]];
//            NSString *myuid=userModel.uid;
            NSString *gender=userModel.gender;
            NSString *age=userModel.age;
            NSDictionary *dic=@{
                                @"fid": @"0",//(家人id)
                                @"gender": gender,//(性别 0:保密 1:男 2:女)
                                @"type": @"0",//(关系id)
                                @"realname":name,//(家人姓名)
                                @"age": age,//(年龄 -1:未填写)
                                @"relation": @"我"//(关系)
                                };
            [self.dataArray insertObject:dic atIndex:0];
            
            for (int i=0; i<[self.dataArray count]; i++) {
                NSString *depar=[NSString stringWithFormat:@"%@",[[self.dataArray objectAtIndex:i] valueForKey:@"fid"]];
                if ( [depar isEqualToString:self.fid]) {
                    dicData=[self.dataArray objectAtIndex:i];
                }
            }

            [self.tableview reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
}
#pragma mark UITableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.dataArray.count;
    }else return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *indentifer=@"cell";
    DepartmentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell==nil) {
        if (kScreenWidth==414) {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"DepartmentTableViewCell" owner:self options:nil] lastObject];
        }else{
            cell=[[[NSBundle mainBundle] loadNibNamed:@"DepartmentTableViewCell" owner:self options:nil] firstObject];
        }
    }
    if (self.dataArray.count>0) {
        NSDictionary *dic=[self.dataArray objectAtIndex:indexPath.row];
        if (indexPath.section==0) {
            cell.departmentName.text=[NSString stringWithFormat:@"%@",[dic valueForKey:@"realname"]];
            
            cell.OutpatientLabel.text=[NSString stringWithFormat:@"(%@)",[dic valueForKey:@"relation"]];
            if ([self.fid isEqualToString:[NSString stringWithFormat:@"%@",[dic valueForKey:@"fid"]]]) {
                cell.selectedImage.hidden=NO;
            }else
            {
                cell.selectedImage.hidden=YES;
            }
        }else
        {
            cell.departmentName.text=@"添加家人";
            cell.selectedImage.hidden=YES;
        }
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        DepartmentTableViewCell *cell=(DepartmentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//        sel_count=indexPath.row;
        //    departmentData=[self.dataArray objectAtIndex:indexPath.row];
        dicData=[self.dataArray objectAtIndex:indexPath.row];
        self.fid=[NSString stringWithFormat:@"%@",[[self.dataArray objectAtIndex:indexPath.row] valueForKey:@"fid"]];
        [tableView reloadData];
        cell.selectedImage.hidden=NO;
    }else
    {
        if (self.dataArray.count-1<18) {
            AddFamilyViewController *addFamily=[[AddFamilyViewController alloc]initWithNibName:@"AddFamilyViewController" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:addFamily animated:YES];
        }else{
            [self showCustomHudSingleLine:@"家人个数已达上限"];
        }
    }

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        DepartmentTableViewCell *cell=(DepartmentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell.selectedImage.hidden=YES;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth==414) {
        return 53;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return 10;
    }else return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    return view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
